package com.ruoyi.project.wenyanwen.data.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.wenyanwen.data.domain.UserData;
import com.ruoyi.project.wenyanwen.data.service.IUserDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户特征Controller
 *
 * @author shixiangwen
 * @date 2020-08-12
 */
@Controller
@RequestMapping("/wenyanwen/data")
public class UserDataController extends BaseController {
    private String prefix = "wenyanwen/data";

    @Autowired
    private IUserDataService userDataService;

    @RequiresPermissions("wenyanwen:data:view")
    @GetMapping()
    public String data() {
        return prefix + "/data";
    }

    /**
     * 查询用户特征列表
     */
    @RequiresPermissions("wenyanwen:data:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(String uuid) {

        UserData data = userDataService.selectUserDataByUuid(uuid);
        if (data == null) {
            return AjaxResult.error("用户特征不存在");
        } else {
            return AjaxResult.success("ok", userDataService.formatFeature(data.getData()));
        }
    }

    /**
     * 导出用户特征列表
     */
    @RequiresPermissions("wenyanwen:data:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserData userData) {
        List<UserData> list = userDataService.selectUserDataList(userData);
        ExcelUtil<UserData> util = new ExcelUtil<UserData>(UserData.class);
        return util.exportExcel(list, "data");
    }

    /**
     * 新增用户特征
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户特征
     */
    @RequiresPermissions("wenyanwen:data:add")
    @Log(title = "用户特征", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserData userData) {
        return toAjax(userDataService.insertUserData(userData));
    }

    /**
     * 修改用户特征
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        return prefix + "/edit";
    }

    /**
     * 修改保存用户特征
     */
    @RequiresPermissions("wenyanwen:data:edit")
    @Log(title = "用户特征", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserData userData) {
        return toAjax(userDataService.updateUserData(userData));
    }

    /**
     * 删除用户特征
     */
    @RequiresPermissions("wenyanwen:data:remove")
    @Log(title = "用户特征", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(userDataService.deleteUserDataByIds(ids));
    }
}
